﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DataAccess.DataObject;
using System.Collections.Generic;

namespace EnmineKaitlin
{
    public partial class search : Common.PageBase
    {
        string word = "";
        int currentpage = 1;
        int pagesize = 20;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {

                if (Request.Params["s"] != null && Request.Params["s"].ToString() != "")
                {
                    GetParam();
                    Search();
                }
                else {
                    Response.Redirect("~/home");
                }
            }
        }
        protected void GetParam()
        {
            if (Request["currentpage"] != null && Request["currentpage"].ToString() != "")
            {
                currentpage = int.Parse(Request["currentpage"].ToString());
            }
            if (Request["s"] != null && Request["s"].ToString() != "")
            {
                word = Request["s"].ToString();
            }
        }
        protected void Search()
        {
            IList<OPost> lstPosts = _BPost.Get(word, "publish", "", "post", 0, "DESC", "CreateDate", currentpage, pagesize);
            if (lstPosts.Count > 0)
            {
                lblTitle.Text = "Kết quả tìm kiếm với từ khóa <span class='word'>'" + word + "'</span>: " + lstPosts[0].TotalResult;
            }
            else { lblTitle.Text = "Kết quả tìm kiếm với từ khóa <span class='word'>'" + word + "'</span>: 0"; }
            rptListNews.DataSource = lstPosts;
            rptListNews.DataBind();           
            //Bind pageging
            if (lstPosts.Count > 0)
            {
                ctlPagging.PageSize = pagesize;
                ctlPagging.ItemCount = (int)lstPosts[0].TotalResult;                
                ctlPagging.QueryStringParameterName = "";
                ctlPagging.CurrentIndex = currentpage;
            }
        }
    }
}
